import sys
import time

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from DButils import select
from DButils import update
from common1.funds_readdata import get_data
from common1.login_operation import *
from common1.funds_operation import *


class Testfunds:

    @pytest.fixture(scope="function", autouse=True)
    def setup_teardown(self):
        service = Service("chromedriver.exe")
        self.driver = webdriver.Chrome(service=service)
        self.driver.get("http://localhost:90")
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        yield
        self.driver.quit()

    #零钱理财
    #正常买入零钱理财-支付宝零钱理财
    @pytest.mark.parametrize("username, password, mima, expect", get_data(0))
    def test_normal_zfb(self, username, password, mima, expect):
        sql = "update bankcard SET balance = %s where userID = %s and cardBank = %s"
        param = [1000000.00, 1, "中国建设银行"]
        update(sql, param)

        login_normal(self.driver, username, password)
        zfb(self.driver, mima)

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [1, 999900.00]
        expect = len(select(sql, param))
        assert expect == 1

    #错误支付密码-买零钱理财-支付宝零钱理财
    @pytest.mark.parametrize("username, password, mima, expect", get_data(1))
    def test_error(self, username, password, mima, expect):
        sql = "update bankcard SET balance = %s where userID = %s and cardBank = %s"
        param = [1000000.00, 1, "中国建设银行"]
        update(sql, param)

        login_normal(self.driver, username, password)
        zfb(self.driver, mima)

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [1, 1000000.00]
        expect = len(select(sql, param))
        assert expect == 1

    #余额不足买入理财产品-支付宝零钱理财
    @pytest.mark.parametrize("username, password, mima, expect", get_data(2))
    def test_not_enough_zfb(self, username, password, mima, expect):
        login_normal(self.driver, username, password)
        zfb(self.driver, mima)

        result = self.driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [9, 90.00]
        expect = len(select(sql, param))
        assert expect == 1

    #未绑定银行卡-买入零钱理财-支付宝零钱理财
    @pytest.mark.parametrize("username, password, mima, expect", get_data(3))
    def test_not_card(self, username, password, mima, expect):
        login_normal(self.driver, username, password)
        zfb(self.driver, mima)

        result = self.driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect

    #期限理财
    #正常买入期限理财-支付宝期限理财
    @pytest.mark.parametrize("username, password, mima, expect", get_data(0))
    def test_normal_qx(self, username, password, mima, expect):
        sql = "update bankcard SET balance = %s where userID = %s and cardBank = %s"
        param = [1000000.00, 1, "中国建设银行"]
        update(sql, param)

        login_normal(self.driver, username, password)
        qx(self.driver, mima)

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [1, 999640.00]
        expect = len(select(sql, param))
        assert expect == 1

    #错误支付密码-买期限理财-支付宝期限理财
    @pytest.mark.parametrize("username, password, mima, expect", get_data(1))
    def test_qx_error(self, username, password, mima, expect):
        sql = "update bankcard SET balance = %s where userID = %s and cardBank = %s"
        param = [1000000.00, 1, "中国建设银行"]
        update(sql, param)

        login_normal(self.driver, username, password)
        qx(self.driver, mima)

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [1, 1000000.00]
        expect = len(select(sql, param))
        assert expect == 1

    #余额不足买入期限理财-支付宝期限理财
    @pytest.mark.parametrize("username, password, mima, expect", get_data(2))
    def test_not_enough_qx(self, username, password, mima, expect):
        login_normal(self.driver, username, password)
        qx(self.driver, mima)

        result = self.driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [9, 90.00]
        expect = len(select(sql, param))
        assert expect == 1

    #未绑定银行卡-买入期限理财-支付宝期限理财
    @pytest.mark.parametrize("username, password, mima, expect", get_data(3))
    def test_qxnot_card(self, username, password, mima, expect):
        login_normal(self.driver, username, password)
        qx(self.driver, mima)

        result = self.driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect

    #基金理财
    #正常买入基金理财-广发多元新兴股票
    @pytest.mark.parametrize("username, password, mima, expect", get_data(0))
    def test_normal_qx(self, username, password, mima, expect):
        sql = "update bankcard SET balance = %s where userID = %s and cardBank = %s"
        param = [1000000.00, 1, "中国建设银行"]
        update(sql, param)

        login_normal(self.driver, username, password)
        jijin(self.driver, mima)

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [1, 997000.00]
        expect = len(select(sql, param))
        assert expect == 1

    # 错误支付密码-买基金理财-广发多元新兴股票
    @pytest.mark.parametrize("username, password, mima, expect", get_data(1))
    def test_jijin_error(self, username, password, mima, expect):
        sql = "update bankcard SET balance = %s where userID = %s and cardBank = %s"
        param = [1000000.00, 1, "中国建设银行"]
        update(sql, param)

        login_normal(self.driver, username, password)
        jijin(self.driver, mima)

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [1, 1000000.00]
        expect = len(select(sql, param))
        assert expect == 1

    # 余额不足买入基金理财-广发多元新兴股票
    @pytest.mark.parametrize("username, password, mima, expect", get_data(2))
    def test_not_enough_jijin(self, username, password, mima, expect):
        login_normal(self.driver, username, password)
        jijin(self.driver, mima)

        result = self.driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect

        sql = "select * from bankcard where userID = %s and balance = %s"
        param = [9, 90.00]
        expect = len(select(sql, param))
        assert expect == 1

    # 未绑定银行卡-买入基金理财-广发多元新兴股票
    @pytest.mark.parametrize("username, password, mima, expect", get_data(3))
    def test_qxnot_card(self, username, password, mima, expect):
        login_normal(self.driver, username, password)
        jijin(self.driver, mima)

        result = self.driver.find_element(By.XPATH, "//*[@id='jconfirm-box51973']/div").text
        assert result == expect

    #银行卡支付测试
    #新银行卡没有设置密码-支付测试
    @pytest.mark.parametrize("username, password, mima, expect", get_data(4))
    def test_no_password_error(self, username, password, mima, expect):
        login_normal(self.driver, username, password)
        jijin(self.driver, mima)

        result = self.driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect

    #多个银行卡-默认支付测试
    @pytest.mark.parametrize("username, password, mima, expect", get_data(5))
    def test_default(self, username, password, mima, expect):
        sql = "update bankcard SET balance = %s where userID = %s and cardBank = %s"
        param = [1000000.00, 8, "中国工商银行"]
        update(sql, param)

        login_normal(self.driver, username, password)
        jijin(self.driver, mima)

        sql = "select * from bankcard where userID = %s and balance = %s and defaultl = %s"
        param = [8, 997000.00, 1]
        expect = len(select(sql, param))
        assert expect == 1

    #多个银行卡-默认支付测试-切换
    @pytest.mark.parametrize("username, password, mima, expect", get_data(6))
    def test_default_qh(self, username, password, mima, expect):
        sql = "update bankcard SET defaultl = %s where userID = %s and cardNum = %s"
        param = [1, 8, "0000000000000001"]
        update(sql, param)

        sql = "update bankcard SET defaultl = %s where userID = %s and cardNum = %s"
        param = [0, 8, "0000000000000002"]
        update(sql, param)

        login_normal(self.driver, username, password)
        self.driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[2]/a").click()

        self.driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/a").click()
        self.driver.find_element(By.XPATH, "//*[@id='leftbaraside']/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        self.driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[3]").click()

        sql = "select * from bankcard where cardNum = %s and defaultl = %s"
        param = ["0000000000000002", 1]
        expect = len(select(sql, param))
        assert expect == 1

    #未绑定银行卡-买入基金理财-广发多元新兴股票
    @pytest.mark.parametrize("username, password, mima, expect", get_data(3))
    def test_normal(self, username, password, mima, expect):
        login_normal(self.driver, username, password)
        jijin(self.driver, mima)

        result = self.driver.find_element(By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[3]/div/div").text
        assert result == expect


















